package com.inspur.xiaoju.service.impl;

import com.inspur.xiaoju.constant.RecordStatus;
import com.inspur.xiaoju.dto.CreateSurveyDto;
import com.inspur.xiaoju.exception.BusinessException;
import com.inspur.xiaoju.model.Survey;
import com.inspur.xiaoju.repository.SurveyRepository;
import com.inspur.xiaoju.security.PluginManager;
import com.inspur.xiaoju.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SurveyServiceImpl implements SurveyService {
    
    @Autowired
    private SurveyRepository surveyRepository;
    
    @Autowired
    private PluginManager pluginManager;
    
    @Override
    public Survey create(CreateSurveyDto createSurveyDto, String creatorId, String creator) {
        Survey survey = new Survey();
        survey.setTitle(createSurveyDto.getTitle());
        survey.setDescription(createSurveyDto.getDescription());
        survey.setWorkspaceId(createSurveyDto.getWorkspaceId());
        survey.setCreatorId(creatorId);
        survey.setCreator(creator);
        survey.setCode(createSurveyDto.getCode());
        survey.setSurveyType(createSurveyDto.getSurveyType());
        
        // 生成问卷路径
        String surveyPath = pluginManager.genSurveyPath();
        survey.setSurveyPath(surveyPath);
        
        // 设置状态
        Date now = new Date();
        Survey.Status status = new Survey.Status();
        status.setStatus(RecordStatus.NEW);
        status.setDate(now);
        
        survey.setCurStatus(status);
        
        List<Survey.Status> statusList = new ArrayList<>();
        statusList.add(status);
        survey.setStatusList(statusList);
        
        survey.setCreateDate(now);
        survey.setUpdateDate(now);
        
        return surveyRepository.save(survey);
    }
    
    @Override
    public Survey findById(String id) {
        return surveyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(404, "问卷不存在"));
    }
    
    @Override
    public Survey findBySurveyPath(String surveyPath) {
        return surveyRepository.findBySurveyPath(surveyPath)
                .orElseThrow(() -> new BusinessException(404, "问卷不存在"));
    }
    
    @Override
    public List<Survey> findByWorkspaceId(String workspaceId) {
        return surveyRepository.findByWorkspaceId(workspaceId);
    }
    
    @Override
    public List<Survey> findByCreatorId(String creatorId) {
        return surveyRepository.findByCreatorId(creatorId);
    }
    
    @Override
    public Survey update(String id, Map<String, Object> updates) {
        Survey survey = findById(id);
        
        if (updates.containsKey("title")) {
            survey.setTitle((String) updates.get("title"));
        }
        
        if (updates.containsKey("description")) {
            survey.setDescription((String) updates.get("description"));
        }
        
        if (updates.containsKey("code")) {
            survey.setCode((Map<String, Object>) updates.get("code"));
        }
        
        if (updates.containsKey("surveyType")) {
            survey.setSurveyType((String) updates.get("surveyType"));
        }
        
        survey.setUpdateDate(new Date());
        
        return surveyRepository.save(survey);
    }
    
    @Override
    public void delete(String id) {
        Survey survey = findById(id);
        
        // 逻辑删除，更新状态为已删除
        Date now = new Date();
        Survey.Status status = new Survey.Status();
        status.setStatus(RecordStatus.DELETED);
        status.setDate(now);
        
        survey.setCurStatus(status);
        survey.getStatusList().add(status);
        survey.setUpdateDate(now);
        
        surveyRepository.save(survey);
    }
    
    @Override
    public Survey updateStatus(String id, String status) {
        Survey survey = findById(id);
        
        Date now = new Date();
        Survey.Status newStatus = new Survey.Status();
        newStatus.setStatus(status);
        newStatus.setDate(now);
        
        survey.setCurStatus(newStatus);
        survey.getStatusList().add(newStatus);
        survey.setUpdateDate(now);
        
        return surveyRepository.save(survey);
    }
}